﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.ServiceModel.DomainServices.Client;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using GradeApp.Web;
using GradeApp.Web.Services;

namespace GradeApp.Views
{
    public partial class Students : Page
    {
        private GradeAppDomainContext _gradeAppDomainContext;
        private IEnumerable<Guid> _studentIds;
        private IEnumerable<Web.Students> _selectedStudents;

        public Students()
        {
            InitializeComponent();
            _gradeAppDomainContext = new GradeAppDomainContext();
            PrepareSelectStudentsQuery();
        }

        private void PrepareSelectStudentsQuery()
        {
            _gradeAppDomainContext.Load(_gradeAppDomainContext.GetStudentCoursesQuery(),
                                        op => FindStudentCoursesQuery(op), null);
        }

        private void FindStudentCoursesQuery(LoadOperation<StudentCourses> op)
        {
            _studentIds = op.Entities.Where(studentCourse => studentCourse.CourseId == GradeAppContext.CurrentCourseId).Select(crsStd => crsStd.StudentId);
            if (_studentIds.Count() > 0)
                _gradeAppDomainContext.Load(_gradeAppDomainContext.GetStudentsQuery(), oper => FindSelectedStudents(oper),
                                            null);
        }

        private void FindSelectedStudents(LoadOperation<Web.Students> oper)
        {
            _selectedStudents = oper.Entities.Where(student => _studentIds.Contains(student.Id));
            selectedStudentsDataGrid.ItemsSource = _selectedStudents;
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void studentsDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {

            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        

        private void bAddStudentToCourse_Click(object sender, RoutedEventArgs e)
        {
            var currentItem = studentsDataGrid.SelectedItem as Web.Students;

            var studentCourses = new StudentCourses()
            {
                Id = Guid.NewGuid(),
                CourseId = GradeAppContext.CurrentCourseId,
                StudentId = currentItem.Id
            };

            currentItem.StudentCourses.Add(studentCourses);
            Form.CommitEdit();
            studentsDomainDataSource.SubmitChanges();
            PrepareSelectStudentsQuery();
        }

        private void Form_EditEnded(object sender, DataFormEditEndedEventArgs e)
        {
            var currentItem = Form.CurrentItem as Web.Students;
            currentItem.Id = Guid.NewGuid();           

            Form.CommitEdit();
            studentsDomainDataSource.SubmitChanges();
        }

        private void Form_DeletingItem(object sender, CancelEventArgs e)
        {
            throw new NotImplementedException();
        }

        private void studentCoursesDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {

            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void selectedStudentsDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {
            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }
    }
}
